package com.sh.data.engine.domain.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.authority.service.StatisticService;
import com.sh.data.engine.repository.dao.integration.online.FlinkDataSyncTaskMapper;
import com.sh.data.engine.repository.dao.integration.online.entity.FlinkDataSyncTaskEntity;
import com.sh.data.engine.repository.dao.workspace.index.ProjectStatisticsMapper;
import com.sh.data.engine.repository.dao.workspace.index.entity.ProjectStatisticsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("integrateOnline")
public class IntegrateOnlineStatisticServiceImpl implements StatisticService {

    @Autowired
    private FlinkDataSyncTaskMapper flinkDataSyncTaskMapper;

    @Autowired
    private ProjectStatisticsMapper projectStatisticsMapper;

    @Override
    public Long statistic() {
        return flinkDataSyncTaskMapper.selectCount(new LambdaQueryWrapper<FlinkDataSyncTaskEntity>()
                .eq(FlinkDataSyncTaskEntity::getProjectId, ServletUtils.getProjectId()));
    }

    @Override
    public void update(Long before, Long after) {
        if (!Objects.equals(after, before)) {
            LambdaUpdateWrapper<ProjectStatisticsEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(ProjectStatisticsEntity::getProjectId, ServletUtils.getProjectId());
            wrapper.setSql("integrate_task_num = integrate_task_num + " + (after - before));
            projectStatisticsMapper.update(wrapper);
        }
    }
}
